#include "Hand.h"

void
hand_clear(Hand *hand)
{
  // iterate through Hand of Cards
  unsigned char i;
  for (i = 0; i < 5; i++)
    // and reset each Card to "no card"
    (*hand)[i] = 0;
}

unsigned char
hand_get_score(Hand *hand)
{
  unsigned char score = 0;
  unsigned char aces = 0;

  // iterate through Hand of Cards
  unsigned char i;
  for (i = 0; i < 5; i++) {
    Card card = (*hand)[i];

    // if the current Card is "no card" -> return current score
    if (card == 0)
      return score;

    // get value of the current Card
    unsigned char card_score = card_get_score(card);

    // if current Card is an ace
    if (card_score == 1) {
      // score as 11
      score += 11;
      // .. and increase ace counter
      aces++;
    } else {
      // otherwise score usual Card value
      score += card_score;
    }

    // if score is too high and we have aces on our hand
    if (aces != 0 && score > 21) {
      // score one ace as 1 instead of 11
      score -= 10;
      // and decrease ace counter
      aces--;
    }
  }

  return score;
}
